package gew.system;

import gew.metadata.Queries;
import gew.services.DimensionValueService;
import gew.utils.SqlUtils;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;

public class RequestParameterTreeIndicatorsInit extends RequestParameterTree {

    public RequestParameterTreeIndicatorsInit(HttpServletRequest request, DimensionValueService dvs) {
        super(request, dvs);
    }

    @Override
    void initFilterCode(final HttpServletRequest request) {
        filterCode = (new Integer(Constants.HIER_GROUP_INDICATOR)).toString();
    }

    @Override
    void initFilterRequestType(final HttpServletRequest request) {
        filterRequestType = Constants.FILTER_REQUEST_INIT;
    }

    @Override
    void initUrlTemplate(final HttpServletRequest request) {        
        String countryUrlCode;
        if (request.getParameterMap().containsKey(Parameters.COUNTRY)) {
            countryUrlCode = request.getParameterMap().get(Parameters.COUNTRY)[0];
        } else if (getParams().contains(Parameters.COUNTRY)) {
            countryUrlCode = request.getAttribute(Parameters.COUNTRY).toString();
        } else {
            countryUrlCode = ResourceBundle.getBundle(BUNDLE_NAME, getLocale()).getString(LabelCode.PARAM_DEFAULT_COUNTRY_CODE_VALUE);
        }        
        StringBuilder buf = new StringBuilder();
        buf
                .append(SqlUtils.VERTICAL_BAR)
                .append(getLocaleUrlCode())
                .append(SqlUtils.VERTICAL_BAR)
                .append(Constants.SECTION_INDICATORS)
                .append(SqlUtils.VERTICAL_BAR)
                .append(SqlUtils.AT)
                .append(SqlUtils.EMPTY)
                .append(SqlUtils.AT)
                .append(SqlUtils.VERTICAL_BAR)
                .append(countryUrlCode)
                .append(SqlUtils.VERTICAL_BAR)                
                ;
        urlTemplate = buf.toString();
    }

    @Override
    void initSelectedIdsCS(final HttpServletRequest request, final DimensionValueService dvs) {
        String indicatorUrlCode;
        if (request.getParameterMap().containsKey(Parameters.INDICATOR)) {
            indicatorUrlCode = request.getParameterMap().get(Parameters.INDICATOR)[0];
        } else if (getParams().contains(Parameters.INDICATOR)) {
            indicatorUrlCode = request.getAttribute(Parameters.INDICATOR).toString();
        } else {
            indicatorUrlCode = ResourceBundle.getBundle(BUNDLE_NAME, getLocale()).getString(LabelCode.PARAM_DEFAULT_INDICATOR_CODE_VALUE);
        }
        selectedIdsCS = dvs.getIdsCSByUrlCode(getLocale(), indicatorUrlCode, Queries.IndicatorDimensionValue);
    }

}
